package com.justgame.project.service.system.service;

import com.justgame.project.cloud.common.constant.Const;
import com.justgame.project.cloud.common.util.RedisUtil;
import com.justgame.project.service.api.entity.ChineseArea;

import com.justgame.project.service.system.mapper.UsualMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class UsualService {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private UsualMapper usualMapper;

    public ChineseArea queryAreaByCode(String code) {
        if (StringUtils.isEmpty(code)) {
            return null;
        }
        return usualMapper.queryAreaByCode(code);
    }

    public List<ChineseArea> queryAreaByName(String name, int level) {
        if (StringUtils.isEmpty(name)) {
            return null;
        }
        return usualMapper.queryAreaByName(name, level);
    }

    public List<ChineseArea> queryAreas(int level) {
        if (redisUtil.exists(Const.KEY_REDIS_AREA)) {
            return redisUtil.getCollection(Const.KEY_REDIS_AREA);
        }
        List<ChineseArea> areas = usualMapper.queryAreas(level);
        redisUtil.lRightPush(Const.KEY_REDIS_AREA, areas, RedisUtil.NOT_EXPIRE);
        return areas;
    }


}
